/*
 * @(#)WebColorChooserExample.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.panels;

import org.eoti.awt.ColorUtil;
import org.eoti.awt.WebColor;
import org.eoti.swing.border.BorderUtil;

import javax.swing.*;
import javax.swing.colorchooser.*;
import javax.swing.event.*;
import java.awt.*;

public class WebColorChooserExample
extends JFrame
		implements ChangeListener
{
	public static void main(String[] args)
	{
		JFrame.setDefaultLookAndFeelDecorated(true);
		WebColorChooserExample example = new WebColorChooserExample();
	}

	protected JLabel label;
	protected JColorChooser fg, bg;
	protected WebColorChooserPanel fgWCP, bgWCP;
	protected ColorSelectionModel fgCSM, bgCSM;

	public WebColorChooserExample()
	{
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.getContentPane().setLayout(new BorderLayout());
		label = new JLabel("No Color Choosen");
		label.setOpaque(true); // or background changes won't be visible
		label.setBorder(BorderUtil.createTitledBorder("Preview"));
		this.getContentPane().add(label, BorderLayout.NORTH);

		fgWCP = new WebColorChooserPanel();
		fg = fgWCP.createColorChooser(true);
		fgCSM = fg.getSelectionModel();
		fgCSM.addChangeListener( this );
		fg.setPreviewPanel(new JPanel());
		fg.setBorder(BorderUtil.createTitledBorder("Foreground"));
		this.getContentPane().add(fg, BorderLayout.WEST);

		bgWCP = new WebColorChooserPanel();
		bg = bgWCP.createColorChooser(true);
		bgCSM = bg.getSelectionModel();
		bgCSM.addChangeListener( this );
		bg.setPreviewPanel(new JPanel());
		bg.setBorder(BorderUtil.createTitledBorder("Background"));
		this.getContentPane().add(bg, BorderLayout.EAST);

		this.pack();
		this.setVisible(true);
	}

	public void stateChanged(ChangeEvent e)
	{
		label.setForeground( fg.getColor() );
		label.setBackground( bg.getColor() );

		WebColor fgColor = fgWCP.getWebColor();
		WebColor bgColor = bgWCP.getWebColor();
		label.setText(
				(fgColor==null?"<Custom>":fgColor.toString())
				+ " on " +
				(bgColor == null?"<Custom>":bgColor.toString())
		);
		label.repaint();
	}
}